﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Simple TextBox intellisense test for GrammarIntelliForJs</title>
    <script language="javascript" type="text/javascript" src="../Script/GrammarIntelliForJs.js"></script>
    
    <script language="javascript" type="text/javascript">
        var oIntelli = new GIMachine("testMachine");
        oIntelli.States[0] = GetLiteralPattern("hello world from GrammarIntelliForJs", 0, true);
        // Test a number pattern
        oIntelli.States[1] = new GIMachineWordState("^[0-9]+$", false, 1, "Number", true);
       
        function myCallBackMethod(){
            alert("got callback..");
        }

        function TestIt() {
//            var oState=null;
//            alert("hello");
//            // Test code
////            var oMachine = new GIMachine("TestName");
////            oMachine.States[0] = new GIMachineWordState("testing", 0, "state0", false);
//            //            document.getElementById("txtOutput").value = oMachine.ToXML();
//            oState = GetLiteralPattern("get invoice from date", 0, true);
            //            alert("got literal... ");
//            var Pattern = "testing asdfasdf", sInput = "testing", s2 = Pattern.substring(0, sInput.length);
//            alert(s2 + "\n" + (s2 == sInput).toString());
            document.getElementById("txtOutput").value = oIntelli.ToXML();

        }

        function ShowIntellisense() {
            var sText = document.getElementById("txtUserInput").value, oDiv = document.getElementById("divSuggestions"), sHTML = "", i = 0, oTrace = document.getElementById("txtOutput"), arrSuggestions = null;
            //arrSuggestions = oIntelli.Suggestions(sText, oTrace); // With trace ... good for debugging
            arrSuggestions = oIntelli.Suggestions(sText); // no tracing

            if (arrSuggestions != null && arrSuggestions.length > 0) {
                oTrace.value += arrSuggestions.length + " suggestions for '" + sText + "'";
                for (i = 0; i < arrSuggestions.length; i++) {
                    if (arrSuggestions[i].IsLiteral) {
                        sHTML += "<b>L:</b> &nbsp;" + arrSuggestions[i].Pattern + "<br>";
                    } else {
                        sHTML += "<b>R:</b> &nbsp;" + arrSuggestions[i].Pattern + "  [" + arrSuggestions[i].Name   + "]<br>";
                    }
                }
                oDiv.innerHTML = sHTML;
            } else {
                oTrace.value += "0 suggestions for '" + sText + "'";
                oDiv.innerHTML = "";
            }
        }

        function ParseText() {
            alert("processText - 1");
            var sText = document.getElementById("txtUserInput").value, oDiv = document.getElementById("divSuggestions"), oTrace = document.getElementById("txtOutput"), oMatchCollection = null;
            //oMatchCollection = oIntelli.ProcessText(sText, oTrace);
            alert("processText - 2");
            oMatchCollection = oIntelli.ProcessText(sText);
            if (oMatchCollection != null) {
                oTrace.value = oMatchCollection.ToXML();
            } else {
                oTrace.value = "No matches...";
            }
        }
        
        
    </script>
</head>
<body onload="TestIt();">
    hello...<br />
    <input type="text" id="txtUserInput" onkeyup="ShowIntellisense()" />&nbsp;<input type="button" value="Parse" onclick="ParseText();" /><br />
    <div id="divSuggestions"></div>
    
    <textarea id="txtOutput" rows="30" cols="80"></textarea>
</body>
</html>
